package accounts;

/**
 *  @author AaronFrost
 */
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import powerpass.PowerPassSQL;
import java.sql.*;

public class StandardAccount extends BaseAccount implements java.io.Serializable{



    public StandardAccount()
    {
        super.setAccountIsNew(true);
    }
    public StandardAccount(String inAccountID)
    {
        getAccountInfo(inAccountID);
        getPasswordHistory(inAccountID);
    }
    public StandardAccount(int accountid, boolean active, Date createddt, Date modifieddt, String name, String username, String description, int accounttype )
    {
        setAccountID(accountid);
        setActive(active);
        setCreated(createddt);
        setLastMod(createddt);
        setAccountName(name);
        setUsername(username);
        setAccountDescription(description);
        setTypeID(accounttype);
    }
    public StandardAccount(int accountid, int clientid, boolean active, Date createddt, Date modifieddt, String name, String username, String description, int accounttype )
    {
        setClientID(clientid);
        setAccountID(accountid);
        setActive(active);
        setCreated(createddt);
        setLastMod(createddt);
        setAccountName(name);
        setUsername(username);
        setAccountDescription(description);
        setTypeID(accounttype);
    }

    @Override
    public void Persist()
    {

    }
    @Override
    public void Save()
    {
        PowerPassSQL saveSQL = new PowerPassSQL();
        Date cal = new Date();
        java.sql.Date date = new java.sql.Date(cal.getTime());
        if (super.getAccountIsNew())
        {
            //new account stuff
        }
        else
        {
            try {
                saveSQL.updateInfo("UPDATE accounts" + " SET clientid='" + this.getClientID() + "', name='" + this.getAccountName() + "', description='" + this.getAccountDescription() + "', username='" + this.getUsername() + "', modifieddt='" + date.toString() + "', active=" + this.getActive() + ", accounttype=" + this.getTypeID() + " WHERE accountid='" + this.getAccountID() + "';");
                if (this.getPasswordHasChanged() == true) {
                    saveSQL.updateInfo("INSERT INTO accountpasshistory (accountid, password, startdate, enddate) "
                                        + "VALUES (" + this.getAccountID()+",'" + this.getPassword() + "', now(), '0000-00-00 00:00:00');");
                    this.setPasswordHasChanged(false);
                }
            } catch (SQLException ex) {
                Logger.getLogger(StandardAccount.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    private void getAccountInfo(String inAccountID) {
        throw new UnsupportedOperationException("Not yet implemented");
        //Select From the database the following information from the Accounts.DB where the AccountID = inAccountID
            /**
             * typeID
             * clientID
             * accountName
             * accountDescription
             * username
             * createdDT
             * lastModDT
             */

        //Select password from passwordhistory.db
        //where accountID = accountID
        //passwordhistoryid = (select max(passwordhistoryid) from passwordhistory.db where accountID = accountID limit 1)

    }

    private void getPasswordHistory(String inAccountID) {
        throw new UnsupportedOperationException("Not yet implemented");
        //Select from the passwordhistory.db the max(passwordid) where accountid = inAccountID;
            /**
             * passwordhistoryid
             * password
             * startDT
             * endDT
             *
             * order by passwordhistoryid desc
             */
        //Put the results of the last query into the passwordhistory array
    }

    
}
